package org.eclipse.rally.plugin.views;

import com.rallydev.webservice.v1_12.domain.Task;

import org.apache.log4j.Logger;
import org.eclipse.jface.window.ApplicationWindow;

import org.eclipse.rally.plugin.Activator;
import org.eclipse.rally.plugin.utils.SWTResourceManager;
import org.eclipse.rally.plugin.utils.UserUtil;
import org.eclipse.rally.plugin.webservice.RallyMain;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;


public class TaskTabGUI extends ApplicationWindow {
    public static Group grpMyTasks;
    public static int minTaskVal = 1;
    public static int maxTaskVal = 10;
    public static int taskPageNumber = 1;
    public static int totalTasks;
    public static Table table_3;
    public static List lstTasks;
    public static Text text_3;
    public static Text text_5;
    public static CCombo combo3;
    public static CCombo combo4;
    public static TabItem tabItem;
    final Display display = Display.getCurrent();
    final Cursor handCursor = new Cursor(display, SWT.CURSOR_HAND);
    //final Cursor handCursor = new Cursor(display, SWT.CURSOR_HAND);
    final Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
    private Logger logger = Activator.getLogManager().getLogger(TaskTabGUI.class.getName());
    static Map refTaskMap;

    public TaskTabGUI() {
        super(null);
    }

    public static void main(String[] args) {
        //parent.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
        Display display = new Display();
        Shell shell = new Shell(display);

        TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
        tabFolder.setBounds(0, 0, 950, 500);

        //dashBoardGUI();
        Composite composite = new Composite(tabFolder, SWT.NONE);
        TaskTabGUI gui = new TaskTabGUI();
        gui.TaskTabData(tabFolder,true);
        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public void TaskTabData(final TabFolder tabFolder,boolean tabObjectChk) {
  
    	if(tabObjectChk){
    		tabItem = new TabItem(tabFolder, SWT.NONE);
    		tabItem.setText("&Task");
    	}
    	Composite composite = new Composite(tabFolder, SWT.NONE);
        /*ScrolledComposite scrolledComposite = new ScrolledComposite(tabFolder, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        tabItem.setControl(scrolledComposite);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
        
        Composite composite_1 = new Composite(scrolledComposite, SWT.BORDER);
        */
       
        tabItem.setControl(composite);
        grpMyTasks = new Group(composite, SWT.BORDER);
        grpMyTasks.setBounds(2, 25, 850, 300);
        grpMyTasks.setText("My Tasks");
        grpMyTasks.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));

        {
            {
                Label label2 = new Label(grpMyTasks, SWT.CENTER);
                label2.setText("Views :");
                label2.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_WIDGET_DARK_SHADOW));
                label2.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                label2.setBounds(10, 23, 36, 13);
            }

            {
                CCombo combo2 = new CCombo(grpMyTasks, SWT.BORDER);
                combo2.setText("Quick Views");
                combo2.setItems(new String[] {
                        "All Defects", "High Priority(p1-p3)",
                        "Scheduled Defects", "Unscheduled Defects",
                        "Found During Current Iteration",
                        "Found During Current Release", "Doc or Relase Note",
                        "Passing Test Cases"
                    });
                combo2.setEditable(false);
                combo2.setBackground(SWTResourceManager
                		.getColor(SWT.COLOR_LIST_BACKGROUND));
                combo2.setBounds(52, 23, 105, 21);
            }

            {
                Label label22 = new Label(grpMyTasks, SWT.CENTER);
                label22.setText("ID");
                label22.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_TITLE_BACKGROUND));
                label22.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                label22.setBounds(0, 60, 36, 13);
            }

            {
                Label lblRelese = new Label(grpMyTasks, SWT.NONE);
                lblRelese.setText("Release");
                lblRelese.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_TITLE_BACKGROUND));
                lblRelese.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                lblRelese.setAlignment(SWT.CENTER);
                lblRelese.setBounds(140, 60, 49, 13);
            }

            {
                Label label33 = new Label(grpMyTasks, SWT.NONE);
                label33.setText("State");
                label33.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_TITLE_BACKGROUND));
                label33.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_LIST_BACKGROUND));
                label33.setBounds(278, 60, 65, 13);
            }

            text_3 = new Text(grpMyTasks, SWT.BORDER);
            text_3.setBounds(10, 79, 76, 19);
            combo3 = new CCombo(grpMyTasks, SWT.BORDER);
            combo3.setText("All");
            combo3.setItems(new String[] {
                    "All", "Release 1.0 (Wave 1)", "Release 1.1", "Release 1.2",
                    "Release 2.0 (Wave 2)"
                });
            combo3.setEditable(false);
            combo3.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            combo3.setBounds(144, 79, 88, 21);
            combo4 = new CCombo(grpMyTasks, SWT.BORDER);
            combo4.setText("All");
            combo4.setItems(new String[] {
                    "All", "Defined", "In-Progress", "Completed"
                });
            combo4.setEditable(false);
            combo4.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            combo4.setBounds(278, 79, 88, 21);

            {
                final Label clbl_Filter = new Label(grpMyTasks, SWT.NONE);
                clbl_Filter.setImage(SWTResourceManager.getImage(
                        DashboardGUI.class, "/icons/Search-enabled.gif"));
                clbl_Filter.setBounds(395, 79, 58, 23);
                clbl_Filter.setBackground(SWTResourceManager
        				.getColor(SWT.COLOR_LIST_BACKGROUND));
                clbl_Filter.addMouseListener(new MouseAdapter() {
                        public void mouseDown(MouseEvent e) {
                            taskPageNumber = 1;
                            text_5.setText(taskPageNumber + "");
                            minTaskVal = 1;

                            populateTasks();
                        }
                    });
                clbl_Filter.setCursor(handCursor);
            }

            {
                Label label55 = new Label(grpMyTasks,
                        SWT.SEPARATOR | SWT.HORIZONTAL);
                label55.setForeground(SWTResourceManager.getColor(
                        SWT.COLOR_DARK_BLUE));
                label55.setBackground(SWTResourceManager.getColor(
                        SWT.COLOR_DARK_BLUE));
                label55.setAlignment(SWT.CENTER);
                label55.setBounds(10, 104, 446, 2);
            }

            table_3 = new Table(grpMyTasks, SWT.BORDER | SWT.FULL_SELECTION);
            table_3.setLinesVisible(true);
            table_3.setHeaderVisible(true);
            table_3.setBounds(11, 104, 820, 160);
            table_3.addListener(SWT.MouseDoubleClick,
                    new Listener() {
                        public void handleEvent(Event e) {
                        	logger.info("*****Tasks Details Initiated");
                        	System.out.println("*****Tasks Details Initiated");
                        	setCurssor(grpMyTasks,true);
                            int id = 2;
                            TableItem[] items = table_3.getItems();
                            if (items.length == 0) {
                                JOptionPane.showMessageDialog(null,
                                    "Nothing to Display", "", 0);
                            } else {
                                TableItem tableItemStr = null;
                                TableItem[] selection = table_3.getSelection();
                                tableItemStr = (TableItem) selection[selection.length -
                                    1];

                                String taskReference = (String) refTaskMap.get(tableItemStr.getText());
                                grpMyTasks.dispose();

                                try {
                                    new TaskDetailGUI().taskDetail(tabFolder,
                                    		tabItem, taskReference,"Task");
                                } catch (Exception e1) {
                                    e1.printStackTrace();
                                   logger.error(e1);
                                }
                            }
                            setCurssor(grpMyTasks,false);
                            logger.info("*****Task Details Finished");
                        }
                    });

            {
                TableColumn tableColumn11 = new TableColumn(table_3, SWT.NONE);
                tableColumn11.setWidth(200);
                tableColumn11.setText("ID");
            }

            {
                TableColumn tblclmnName = new TableColumn(table_3, SWT.NONE);
                tblclmnName.setWidth(205);
                tblclmnName.setText("Name");
            }

            {
                TableColumn tblclmnStatus_1 = new TableColumn(table_3, SWT.NONE);
                tblclmnStatus_1.setWidth(205);
                tblclmnStatus_1.setText("Status");
            }

            {
                TableColumn tblclmnOwner_1 = new TableColumn(table_3, SWT.NONE);
                tblclmnOwner_1.setWidth(205);
                tblclmnOwner_1.setText("Owner");
            }

            populateTasks();

            final CLabel clbl_First = new CLabel(grpMyTasks, SWT.NONE);
            clbl_First.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_First.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/Button-MoveAll.gif"));
            clbl_First.setBounds(470, 270, 50, 17);
            clbl_First.setCursor(handCursor);
            clbl_First.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        int x = totalTasks % maxTaskVal;
                        int y = (totalTasks / maxTaskVal);

                        if (x == 0) {
                            taskPageNumber = y;
                        } else {
                            taskPageNumber = y + 1;
                        }

                        minTaskVal = (taskPageNumber * 10) - (maxTaskVal - 1);
                        text_5.setText(taskPageNumber + "");
                        populateTasks();
                    }
                });

            final CLabel clbl_Next = new CLabel(grpMyTasks, SWT.NONE);
            clbl_Next.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_Next.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/FF-enabled.gif"));
            clbl_Next.setBounds(425, 270, 50, 17);
            clbl_Next.setCursor(handCursor);
            clbl_Next.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        if (lstTasks.size() == maxTaskVal) {
                            minTaskVal += maxTaskVal;
                            taskPageNumber++;
                            text_5.setText(taskPageNumber + "");
                        }

                        populateTasks();
                    }
                });

            final CLabel clbl_Last = new CLabel(grpMyTasks, SWT.NONE);
            clbl_Last.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_Last.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/Button-RemoveAll.gif"));
            clbl_Last.setBounds(300, 270, 50, 17);
            clbl_Last.setCursor(handCursor);
            clbl_Last.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        minTaskVal = 1;
                        taskPageNumber = 1;
                        text_5.setText(taskPageNumber + "");
                        populateTasks();
                    }
                });

            final CLabel clbl_Previous = new CLabel(grpMyTasks, SWT.NONE);
            clbl_Previous.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            clbl_Previous.setImage(SWTResourceManager.getImage(
                    DefectsTabGUI.class, "/icons/Button-REW.gif"));
            clbl_Previous.setBounds(360, 270, 50, 17);
            clbl_Previous.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseDown(MouseEvent e) {
                        if (minTaskVal > 1) {
                            minTaskVal -= maxTaskVal;
                            taskPageNumber--;
                            text_5.setText(taskPageNumber + "");
                        }

                        populateTasks();
                    }
                });
            clbl_Previous.setCursor(handCursor);
            text_5 = new Text(grpMyTasks, SWT.BORDER);
            text_5.setBounds(560, 270, 36, 19);
            text_5.setText(taskPageNumber + "");

            Label label99 = new Label(grpMyTasks, SWT.NONE);
            label99.setBounds(530, 270, 49, 13);
            label99.setText("P No :");
            label99.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        }
    }

    private  void populateTasks() {
        try {
            Task task = new Task();

            if (text_3.getText().trim().length() > 0) {
                task.setFormattedID(text_3.getText());
            } else {
                task.setFormattedID(null);
            }

            task.setRelease(null);

            if (!combo4.getText().equalsIgnoreCase("All")) {
                task.setState(combo4.getText());
            } else {
                task.setState(null);
            }

            RallyMain rallyMain = new RallyMain();

            if (lstTasks != null) {
                lstTasks.clear();
            }

            lstTasks = rallyMain.searchObject(task, "Task", minTaskVal,
                    maxTaskVal);
            table_3.removeAll();
            refTaskMap = new HashMap();
            if ((lstTasks != null) && (lstTasks.size() > 0)) {
                for (int i = 0; i < lstTasks.size(); i++) {
                    TableItem tableItem = new TableItem(table_3, SWT.NONE);

                    
                    Task task1 = (Task) lstTasks.get(i);
                  
                    refTaskMap.put(task1.getFormattedID(), task1.getRef());
                    String[] strValue = {
                            task1.getFormattedID(), task1.getName(),
                            task1.getState(), 
                            (String)UserUtil.userMap.get(task1.getOwner())
                        };
                    tableItem.setText(strValue);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setCurssor(Control control, boolean b) {
        if (b) {
        	control.setCursor(waitCursor);
        } else {
        	control.setCursor(handCursor);
        }
    }
    public void backToTaskPage(TabFolder tabFolder) {
        tabFolder = tabFolder;
        TaskTabData(tabFolder,false);
    }
}
